const md5 = require('md5')
const User = require('../../model/user')
const messInfo = require('../messageInfo')
const addUser = async (req, res) => {
    const user_name = await User.findOne({ user_name: req.body.user_name });
    const user_phone = await User.findOne({ user_phone: req.body.user_phone });
    if (user_name || user_phone) {
        res.status(422).json(messInfo(null, 422, "用户名或者手机号已存在"))
    } else {
        try {
            req.body.user_password = md5(req.body.user_password)
            const data = await User.create(req.body)
            res.json(messInfo(data, 201, '添加成功'))
        } catch (err) {
            res.status(400).json(messInfo(null, 400, err.errors))
        }
    }

};
module.exports = addUser;